#include<stdio.h>
#include<unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>

#define PATHNAME "/home/china"
#define PRO_ID 3000


struct msgbuf
{
    long mtype;    /* message type, must be > 0 */
    char mtext[1024]; /* message data   柔性数组 */
};

int main()
{
    //创建一个IPC的key
    key_t key = ftok(PATHNAME,PRO_ID);
	if(key == -1)
	{
		perror("fial");
		return -1;
	}
    //利用key创建一个IPC对象
    int msgid = msgget(key,IPC_CREAT | 0664);
    if(msgid == -1)
    {
        perror("fial");
        return -1;
    }
    //读取消息
    struct msgbuf buf;//用于存放读取数据的空间
    //msgrcv是用来从msqid表示的消息队列中读取复合条件的消息
    ssize_t ret = msgrcv(msgid, &buf, 1024,0,0);
    if(ret == -1)
    {
        perror("fial");
        return -1;
    }
    else
    {
        printf("ret = %ld\n",ret);
        printf("type = %ld\n",buf.mtype);
        printf("data = %s\n",buf.mtext);
    }
}
